var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    rightDatas: [],
    applyinput:'', //申请留言
    isapply:false,  //是否申请
    isbackapply:false,  //是否取消申请
    isdel:false    //是否删除供应商
  },
  // 打电话
  call_phone:function(e){
    if(app.globalData.test==1){
      wx.showLoading({
        title: '暂无权限',
        duration: 500
      });
      return;
    };
    let phoneNumber;
    if(this.data.rightDatas.dockerPhone){
      phoneNumber=this.data.rightDatas.dockerPhone;
    }else if(this.data.rightDatas.alias_id>0){
      phoneNumber=this.data.rightDatas.alias_phone;
    }else{
      phoneNumber=this.data.rightDatas.phone;
    };
    wx.makePhoneCall({
      phoneNumber: phoneNumber
    })
  },
  // 打开申请合作弹窗
  addsupper:function(){
    let that = this;
    if(that.data.level == 4){ //批发端
      app.AppHttp('api/Supplier/getCoopCustomCount', {  //免费版限制客户
        supplier_id:that.data.id
      }, function (res) {
        console.log(JSON.stringify(res.data));
        if(res.data.supplier_identity==0 && res.data.custom_count>=10){
          wx.showModal({
            title: '提示',
            content: "该供应商已有"+res.data.custom_count+"个客户\n供应商版本限定\n客户总量最多10个",
            showCancel: false,
            confirmText:"我知道了",
            success: function (res) {}
          });
        }else{
          that.setData({
            isapply:true
          });
        };
      });
    }else{
      that.setData({
        isapply:true
      });
    };
  },
  // 关闭申请合作弹窗
  apply_cancel:function(){
    this.setData({
      isapply: false
    })
  },
  // 确定申请合作
  apply_confirm:function(){
    var that = this;
    that.setData({
      isapply: false
    });
    var message_type;
    if (that.data.level == 1) {  //合作供应商端
      message_type = 'add_info';
    } else if (that.data.level == 4) { //合作批发端
      message_type = 'pf_add_info';
    };
    // 添加供应商接口
    wx.request({
      url: app.globalData.url + 'api/Client/AddSupplier',
      method: "POST",
      data: {
        supplier_id: that.data.id,
        openid: app.globalData.openid
      },
      success: function (res) {
        wx.showToast({
          title: res.data.msg,
        }, 2000);
        // 向供应商发送申请通知
        wx.request({
          url: app.globalData.url + 'api/Message1/add_info',
          method: 'GET',
          data: {
            client_id: app.globalData.loginInfo.id,
            supplier_id: that.data.id,
            type: 1,   //1为请求，2为同意
            message_type: message_type
          },
          success: function (res) {
            console.log(JSON.stringify(res.data));
            if (that.data.is_auto_agree == 1) {
              wx.request({
                url: app.globalData.url + 'api/Message1/add_info',
                method: 'GET',
                data: {
                  client_id: app.globalData.loginInfo.id,
                  supplier_id: that.data.id,
                  type: 2  //1为请求，2为同意
                },
                success: function (res) {
                  console.log(JSON.stringify(res.data));
                  that.supperdetail(2);
                }
              });
            }
          }
        });       
        that.setData({
          is_coop:1,
          status:-1
        })
      }
    });
  },
  // 打开取消申请弹窗
  quxiao_apply:function(){
    this.setData({
      isbackapply: true
    })
  },
  // 关闭取消申请弹窗
  back_cancel:function(){
    this.setData({
      isbackapply: false
    })
  },
  // 确定取消申请
  apply_hezuo:function(){
    this.setData({
      isbackapply: false
    })
    this.stopcopper();
  },
  // 点击刷新，查看申请状态
  refresh_but:function(){
    this.supperdetail(2);
  },
  // 打开删除供应商弹窗
  del_apply: function () {
    this.setData({
      isdel: true
    })
  },
  // 关闭删除供应商弹窗
  del_cancel: function () {
    this.setData({
      isdel: false
    })
  },
  // 确定删除供应商
  del_confirm: function () {
    this.setData({
      isdel: false
    })
    this.stopcopper();
  },
  stopcopper:function(){
    var that = this;
    app.AppHttp('api/Client/del_cooper', {
      id: that.data.copper_id
    }, function (res) {
      console.log(JSON.stringify(res.data));
      wx.showToast({
        title:res.data.msg
      });
      wx.switchTab({
        url:"/pages/shangpin/shangpin"
      });
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options,query) {
    var that = this;
    console.log(options);
    if (options.is_coop){
      that.setData({
        is_coop: options.is_coop
      });
    };
    console.log(app.globalData.openid);
    that.setData({
      id:options.id,
      group_id: app.globalData.group_id  //是否绑定了集团码
    })
    that.supperdetail(1);
  },
  // 供应商详情
  supperdetail:function(a){
    var that = this;
    app.AppHttp('api/Client/supperDetail', {  
      openid: app.globalData.openid,
      id: that.data.id
    }, function (res) {
      if(!res.data.data.order_start_time || res.data.data.order_start_time=='null' || res.data.data.order_start_time=='0'){
        res.data.data.order_start_time='';
      };
      if(!res.data.data.order_end_time || res.data.data.order_end_time=='null' || res.data.data.order_end_time=='0'){
        res.data.data.order_end_time='';
      };
      that.setData({
        rightDatas: res.data.data,
        status: res.data.data.status,
        level: res.data.data.level,
        copper_id: res.data.data.copper_id,
        is_auto_agree: res.data.data.is_auto_agree
      });
      if (a == 2 && res.data.data.status==1){
        wx.navigateBack({
          delta: 1
        });
      } else if (a == 2 && res.data.data.status==-1){
          wx.showLoading({
            title: '请等待',
            duration: 500
          });
      }
    })
  },
  telClick:function(){
    var that = this;
    var phone = that.data.rightDatas.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  changeSwitch:function(){
    var that =  this;
    var copper_id = that.data.copper_id;
    var status = that.data.status;
    app.AppHttp('api/Client/changeCopperStatus', {
      copper_id: copper_id,
      status: status
    }, function (res) {
      if (status == 0) {
        status = 1;
      } else {
        status = 0;
      };
      that.setData({
        status: status
      })
      console.log(JSON.stringify(res.data))
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})